package com.geek.video.config;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * 本地缓存的配置类
 *
 * @author Steven
 */
@Configuration
public class CacheConfig {

    /**
     * CacheManager 缓存管理器
     *
     * @return CacheManager
     */
    @Bean("writeCacheManager")
    public CacheManager writeCacheManager() {

        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCaffeine(Caffeine.newBuilder()
                // 设置最后一次写入后1天内失效
                .expireAfterWrite(1, TimeUnit.DAYS)
                // 初始的缓存空间大小 100 条
                .initialCapacity(100)
                // 缓存的最大条数 1000 条
                .maximumSize(1000));

        return cacheManager;
    }
}
